//
//  OECardVertifyViewController.m
//  OneEightyNine
//
//  Created by Mac on 2019/3/7.
//  Copyright © 2019 stoneobs. All rights reserved.
//

#import "OECardVertifyViewController.h"
#import "OEBankListViewController.h"
@interface OECardVertifyViewController ()<STSendButtonDlegate>
@property(nonatomic, strong) UITextField                     *phoneTextFiled;/**<  */
@property(nonatomic, strong) UITextField                     *codeTextFiled;/**<  */
@property(nonatomic, strong) STSendButton                     *sendButton;/**<  */
@end

@implementation OECardVertifyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self configSubView];
    self.title = OESTRING(@"Bank Card INFORMATION");
    [self configSubView];
    // Do any additional setup after loading the view.
}
#pragma mark --subView
- (void)configSubView{
    __weak typeof(self) weakSelf =  self;
    self.tableView.backgroundColor = OE_GrayColor;
    UIView * footer = [[UIView alloc] initWithFrame:CGRectMake(0, 0, UIScreenWidth, 100)];
    footer.backgroundColor = self.tableView.backgroundColor;
    
    self.sendButton = [[STSendButton alloc] initWithFrame:CGRectMake(0, 0, 74, 24) andWithDuration:60];
    [self.sendButton st_setBorderWith:0.5 borderColor:TM_YellowBackGroundColor cornerRadius:2];
    self.sendButton.layer.cornerRadius = 2;
    self.sendButton.clipsToBounds = YES;
    self.sendButton.delegate = self;
    [self.sendButton setTitleColor:SecendTextColor forState:UIControlStateNormal];
    [self.sendButton setTitleColor:SecendTextColor forState:UIControlStateSelected];
    [self.sendButton setTitle:@"CODE" forState:UIControlStateNormal];
    self.sendButton.titleLabel.font = [UIFont systemFontOfSize:10];
    self.sendButton.backgroundColor = UIColor.whiteColor;
    [self.sendButton st_setBorderWith:0.5 borderColor:TM_lineColor cornerRadius:12];
    
    self.phoneTextFiled = [TMUtils textFiledWithLeftTitle:@"PHONE NUMBER" placeHolader:@"" rightView:self.sendButton];
    self.phoneTextFiled.top = 10;
    self.phoneTextFiled.backgroundColor = UIColor.whiteColor;
    self.phoneTextFiled.keyboardType = UIKeyboardTypeNumberPad;
    [self.phoneTextFiled st_showBottomLine];
    [footer addSubview:self.phoneTextFiled];
    
    self.codeTextFiled = [TMUtils textFiledWithLeftTitle:@"CAPTCHA" placeHolader:@"" rightView:nil];
    self.codeTextFiled.top = self.phoneTextFiled.bottom;;
    self.codeTextFiled.backgroundColor = UIColor.whiteColor;
    self.codeTextFiled.keyboardType = UIKeyboardTypeNumberPad;
    
    [footer addSubview:self.codeTextFiled];
    self.codeTextFiled.leftView.width = 170;
    self.phoneTextFiled.leftView.width = 170;
    
    STButton * buyButton = [[STButton alloc] initWithFrame:CGRectMake(6, self.codeTextFiled.bottom, 200, 44)
                                                     title:@"View 《User Agreement》"
                                                titleColor:FirstTextColor
                                                 titleFont:15
                                              cornerRadius:0
                                           backgroundColor:nil
                                           backgroundImage:nil
                                                     image:nil];
    [footer addSubview:buyButton];
    [buyButton setAttributedTitle:[buyButton.currentTitle st_convertAttributeStringWithKeyWord:@"《User Agreement》" attributes:@{NSForegroundColorAttributeName:FlatBlue}] forState:UIControlStateNormal];
    
    STButton * confimButton = [[STButton alloc] initWithFrame:CGRectMake(30, buyButton.bottom + 30, UIScreenWidth - 60 , 44)
                                                        title:@"COMPLETE"
                                                   titleColor:[UIColor whiteColor]
                                                    titleFont:18
                                                 cornerRadius:22
                                              backgroundColor:OE_LIMITE_COLOR
                                              backgroundImage:nil
                                                        image:nil];
    [confimButton setClicAction:^(UIButton *sender) {
        [weakSelf onSelctedConfimButton];
    }];
    confimButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentCenter;
    [footer addSubview:confimButton];
    
    footer.height = confimButton.bottom  +20;
    self.tableView.tableFooterView = footer;
    

}
#pragma mark --STSendButtonDlegate
- (BOOL)st_sendButtonWillClic:(UIButton*)button{
    return YES;
}
- (void)st_sendButtonDidClic:(UIButton*)button isFirstClic:(BOOL)isFirstClic duration:(NSInteger)duration{
    if (isFirstClic) {
        if (!self.phoneTextFiled.text.length) {
            [SVProgressHUD showErrorWithStatus:@"please input phone number"];
            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
                [self.sendButton st_timerEnd];
            });
            return;
        }
        [OERegisterDataController sendGetRegCodeRequestWithPhone:self.phoneTextFiled.text handle:^(BOOL success, NSString *errorString, NSDictionary *resp) {
            if (success) {
                [self.sendButton st_timerBegin];
            }else{
                [self.sendButton st_timerEnd];
            }
        }];
    }
}

- (void)st_sendButtonDidCountdown:(UIButton*)button duration:(NSInteger)duration{
    [self.sendButton setTitle:@(duration).description forState:UIControlStateNormal];
}
- (void)st_sendButtonTimeEnded:(UIButton*)button{
    
    [self.sendButton setTitle:@"重新发送" forState:UIControlStateNormal];
    [self.sendButton st_timerEnd];
    
}
#pragma mark --Action Method
- (void)onSelctedConfimButton{
    [SVProgressHUD showWithStatus:@"请稍后"];
    NSMutableDictionary * paramDic = [NSMutableDictionary new];
    NSString * token = STUserManger.defult.loginedUser.token;
    if (token.length) {
        [paramDic setObject:token forKey:@"user_token"];
    }
    if (self.phoneTextFiled.text.length) {
        [paramDic setObject:self.phoneTextFiled.text forKey:@"mobile"];
    }else{
        [SVProgressHUD showErrorWithStatus:@"please input phone number"];
        return;
    }
    if (self.codeTextFiled.text.length) {
        [paramDic setObject:self.codeTextFiled.text forKey:@"mobile_code"];
    }else{
        [SVProgressHUD showErrorWithStatus:@"please input mobile_code "];
        return;
    }
    if (self.bank_name.length) {
        [paramDic setObject:self.bank_name forKey:@"bank_name"];
    }
    if (self.true_name.length) {
        [paramDic setObject:self.true_name forKey:@"true_name"];
    }
    if (self.account.length) {
        [paramDic setObject:self.account forKey:@"account"];
    }
    if (self.account_type.length) {
        [paramDic setObject:self.account_type forKey:@"account_type"];
    }
    [SVProgressHUD showWithStatus:@"please waitting"];
    NSString * url = [NSString stringWithFormat:@"%@%@",releaseServerUrlHeader,@"index.php?m=Front&c=User&a=bindBankThree"];
    DDLogInfo(@"url:%@",url);
    DDLogInfo(@"param:%@",paramDic);
    [[STNetWrokManger defaultClient] requestWithPath:url
                                              method:STHttpRequestTypePost
                                          parameters:paramDic
                                             success:^(NSURLSessionDataTask *operation, id responseObject) {
                                                 [SVProgressHUD dismiss];
                                                 //                                                 [STNetWrokManger shownNormalRespMsgWithResponse:responseObject];
                                                 [SVProgressHUD showSuccessWithStatus:@"Binding success"];
                                                 for (UIViewController * vc in self.navigationController.childViewControllers) {
                                                     if ([vc isKindOfClass:OEBankListViewController.class]) {
                                                         [self.navigationController popToViewController:vc animated:YES];
                                                     }
                                                 }
                                                 
                                                 // DDLogInfo(@"json = \n%@",modelDic.mj_JSONString);
                                                 //                                                 [self.tableView.mj_header endRefreshing];
                                                 DDLogInfo(@"%@请求成功:resp\n%@",url,responseObject);
                                                 
                                                 
                                             } failure:^(NSString *stateCode, STError *error,NSError *originError) {
                                                 [SVProgressHUD dismiss];
                                                 //                                                 [self.tableView.mj_header endRefreshing];
                                                 [SVProgressHUD showErrorWithStatus:error.desc];
                                                 DDLogError(@"\n请求失败:\nurl:%@\nparam:%@\n失败原因:%@\n错误码:%ld",
                                                            url,paramDic,error.desc,error.code);
                                             }];
}
@end
